<template>
  <div id="app-container" style="padding:30px;">
    <el-form label-width="130px">
      <el-form-item label="书名">
        <el-col :span="5">
          <el-input v-model="book.name"/>
        </el-col>
      </el-form-item>
      <el-form-item label="作者">
        <el-col :span="5">
          <el-input v-model="book.author"/>
        </el-col>
      </el-form-item>
      <!-- TODO 分类 -->
      <!-- TODO 版权 -->
      <!-- TODO 批次 -->
      <!-- TODO 书单 -->
      <el-form-item label="授权开始时间">
        <el-date-picker
          v-model="book.startTime"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item label="授权结束时间">
        <el-date-picker
          v-model="book.endTime"
          type="datetime"
          placeholder="选择结束时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item label="连载">
        <el-select v-model="book.serial" clearable placeholder="请选择">
          <el-option :value="1" label="连载中"/>
          <el-option :value="0" label="已完结"/>
        </el-select>
      </el-form-item>
      <el-form-item label="全本收费">
        <el-select v-model="book.charge" clearable placeholder="请选择">
          <el-option :value="1" label="收费"/>
          <el-option :value="0" label="免费"/>
        </el-select>
      </el-form-item>
      <el-form-item label="原创">
        <el-select v-model="book.original" clearable placeholder="请选择">
          <el-option :value="1" label="原创"/>
          <el-option :value="0" label="非原创"/>
        </el-select>
      </el-form-item>
      <el-form-item label="一级分类">
        <el-select v-model="book.firstSort" placeholder="请选择" @change="getSecondCategoryList">
          <el-option
            v-for="item in firstCategoryList"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="book.secondSort" placeholder="请选择">
          <el-option
            v-for="item in secondCategoryList"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="简介">
        <el-input v-model="book.introduction" :rows="10" type="textarea"/>
      </el-form-item>
      <!-- TODO 书封 -->
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="save()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import book from '@/api/cms/book'
import category from '@/api/cms/category'
export default {
  data() {
    return {
      book: {
        id: 0,
        firstSort: '',
        secondSort: ''
      },
      firstCategoryList: [],
      secondCategoryList: [],
      saveBtnDisabled: false
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        this.view(id)
      }
    },
    view(id) {
      // 根据id进行数据查询,回显数据
      book.view(id)
        .then(response => {
          this.book = response.data.book
          category.getCategoryList()
            .then(response => {
              this.firstCategoryList = response.data.firstCategoryList
              for (let i = 0; i < this.firstCategoryList.length; i++) {
                if(this.firstCategoryList[i].id === this.book.firstSort) {
                  this.secondCategoryList = this.firstCategoryList[i].secondCategoryList
                }
              }
            })
        })
    },
    save() {
      book.update(this.book)
        .then(response => {
          this.$message({
            type: 'success',
            message: '修改成功'
          })
          this.$router.push('/cms/book/list')
        })
    },
    getSecondCategoryList(row) {
      this.book.secondSort = ''
      for (let i = 0; i < this.firstCategoryList.length; i++) {
        if (this.firstCategoryList[i].id === row) {
          this.secondCategoryList = this.firstCategoryList[i].secondCategoryList
        }
      }
    }
  }
}
</script>

